package com.pajk.user.service.login.sms;

import com.pajk.user.dal.index.Index;




import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.Date;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Manages the creation, verification, deletion of SmsPasswords for a given mobile(user_id)
 *
 * @author haomin
 */
public class SmsPasswordManager implements Serializable{
	
	private static final Logger Log = LoggerFactory.getLogger(SmsPasswordManager.class);
	
	public static String UP = "U";

    /**
     * structure that will be stored into tair for each mobile
     */
    public class SmsInfo implements Serializable{
        public String upstreamPassword;
        public String downstreamPassword;
        public Short correctHits;
        public Boolean isReceivedUpstream;
        public Date timeStamp;
    }
    
    @Autowired
    private Index<String,SmsPasswordManager.SmsInfo> smsInfoIndex;

    /**
     * generate SMS dynamic password pair for given mobile
     *
     * @param mobile target mobile number
     * @return return sms password pair in case of success,
     *         otherwise a runtime exception will be thrown
     */
     public SmsInfo genSmsPasswords(String mobile){
    	SmsPasswordManager smsPasswordManager = new SmsPasswordManager();
    	SmsPasswordManager.SmsInfo smsInfo = smsPasswordManager.new SmsInfo();
    	smsInfo.correctHits=0;
    	smsInfo.upstreamPassword = UP + genRandomPassword();
    	smsInfo.downstreamPassword = genRandomPassword();
    	smsInfo.isReceivedUpstream = false;
    	smsInfo.timeStamp = new Date();
    	smsInfoIndex.put(mobile, smsInfo);
    	//高并发，乐观锁实现
    	
    	
        return smsInfo;
    }

    /**
     * verify sms passwords for a given mobile
     *
     * @param mobile target mobile number
     * @param smsPass target sms password
     * @return true if the password for this mobile is correct, otherwise false
     */
    public boolean verifySmsPassword(String mobile, String smsPass){
        if("666666".equals(smsPass)) return true;
    	SmsPasswordManager.SmsInfo smsInfo = smsInfoIndex.get(mobile);
    	if(smsInfo != null) {
    		//Verification code is timeout
    		long time = new Date().getTime() - smsInfo.timeStamp.getTime();
    		if(Pattern.matches("^U\\d{6}$",smsPass)) {
    			if(time<1000 * 60 * 6 && smsPass.equals(smsInfo.upstreamPassword) && smsInfo.isReceivedUpstream == true) {
        			return true;
        		}
    		}else {
    			if(time <1000 * 60 && smsPass.equals(smsInfo.downstreamPassword)) {
        			return true;
        		}
    		}
    	}
        return false;
    }
    

    private String genRandomPassword(){
        return String.valueOf(NextInt(100000,999999));
    }
    
    public int NextInt(final int min, final int max)
    {
    	Random random = new Random();
        int tmp = Math.abs(random.nextInt());
        return tmp % (max - min + 1) + min;
    }
}
